//
//  FilterView.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/19.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "FilterView.h"
#import "EqualCellSpaceFlowLayout.h"

@interface FilterView()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *selectArray;

@end

@implementation FilterView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.dataArray = [NSMutableArray array];
        self.selectArray = [NSMutableArray array];
        
        UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenX375(46), ScreenX375(46))];
        [backBtn setImage:ImageWithName(@"矢量智能对象拷贝") forState:UIControlStateNormal];
        [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:backBtn];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(50), 0, ScreenX375(180), ScreenX375(46))];
        titleLabel.text = @"筛选";
        titleLabel.textColor = FONTCOLOR_BLACK;
        titleLabel.font = Kfont(16);
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:titleLabel];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(46) - 0.5, ScreenX375(280), 0.5)];
        line.backgroundColor = LineColor;
        [self addSubview:line];
        
        EqualCellSpaceFlowLayout *flowLayout = [[EqualCellSpaceFlowLayout alloc] initWithType:AlignWithLeft betweenOfCell:ScreenX375(10)];
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, ScreenX375(46), ScreenX375(280), SCREEN_HEIGHT - ScreenX375(46) - ScreenX375(49)) collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        [_collectionView registerClass:[FilterViewCell class] forCellWithReuseIdentifier:@"FilterViewCell"];
        
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headCell1"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headCell2"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headCell3"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headCell4"];
        
        [self addSubview:_collectionView];
        
        UIButton *resetBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - ScreenX375(49), ScreenX375(140), ScreenX375(49))];
        [resetBtn setTitle:@"重置" forState:UIControlStateNormal];
        [resetBtn setTitleColor:FONTCOLOR_BLACK forState:UIControlStateNormal];
        resetBtn.titleLabel.font = Kfont(14);
        [resetBtn addTarget:self action:@selector(reSet) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:resetBtn];
        
        UIView *tline = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenX375(140), 0.5)];
        tline.backgroundColor = LineColor;
        [resetBtn addSubview:tline];
        
        UIButton *doneBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(140), SCREEN_HEIGHT - ScreenX375(49), ScreenX375(140), ScreenX375(49))];
        [doneBtn setTitle:@"完成" forState:UIControlStateNormal];
        [doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        doneBtn.titleLabel.font = Kfont(14);
        doneBtn.backgroundColor = ThemeColor;
        [doneBtn addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:doneBtn];
    }
    return self;
}

- (void)back {
    if (_backClick) {
        _backClick();
    }
}

- (void)reSet {
    [_selectArray removeAllObjects];
    for (int i = 0; i < _dataArray.count; i++) {
        [_selectArray addObject:[NSString stringWithFormat:@"%lu",[_dataArray[i][@"dataArray"] count]]];
    }
    [_collectionView reloadData];
}

- (void)done {
    if (_doneClick) {
        _doneClick(_selectArray);
    }
}

- (void)modelArrayForView:(NSMutableArray *)array {
    _dataArray = array;
    [_selectArray removeAllObjects];
    for (int i = 0; i < _dataArray.count; i++) {
        [_selectArray addObject:[NSString stringWithFormat:@"%lu",[_dataArray[i][@"dataArray"] count]]];
    }
    [_collectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _dataArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [_dataArray[section][@"dataArray"] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FilterViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FilterViewCell" forIndexPath:indexPath];
    cell.layer.cornerRadius = ScreenX375(15);
    if (indexPath.row == [_selectArray[indexPath.section] integerValue]) {
        cell.backgroundColor = ThemeColor;
        cell.textLabel.textColor = [UIColor whiteColor];
    }else {
        cell.backgroundColor = REXADECIMALCOLOR(@"#eeeeee");
        cell.textLabel.textColor = REXADECIMALCOLOR(@"#999999");
        
    }
    cell.textLabel.text = _dataArray[indexPath.section][@"dataArray"][indexPath.row];
    NSString *key = _dataArray[indexPath.section][@"dataArray"][indexPath.row];
    CGRect rect = [key boundingRectWithSize:CGSizeMake(ScreenX375(200), ScreenX375(30)) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName:Kfont(12)} context:nil];
    cell.textLabel.frame = CGRectMake(0, 0, rect.size.width + ScreenX375(28), ScreenX375(30));
    cell.clipsToBounds = YES;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == [_selectArray[indexPath.section] integerValue]) {
        [_selectArray replaceObjectAtIndex:indexPath.section withObject:[NSString stringWithFormat:@"%ld",[_dataArray[indexPath.section][@"dataArray"] count]]];
    }else {
        [_selectArray replaceObjectAtIndex:indexPath.section withObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
    }
    [_collectionView reloadData];
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(14), ScreenX375(280), ScreenX375(16))];
    noteLabel.text = _dataArray[indexPath.section][@"title"];
    noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
    noteLabel.font = Kfont(14);
    
    UICollectionReusableView *reusableView = nil;
    if(kind == UICollectionElementKindSectionHeader)
    {
        if (indexPath.section == 0) {
            reusableView = [_collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headCell1" forIndexPath:indexPath];
        }else if (indexPath.section == 1) {
            reusableView = [_collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headCell2" forIndexPath:indexPath];
        }else if (indexPath.section == 2) {
            reusableView = [_collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headCell3" forIndexPath:indexPath];
        }else {
            reusableView = [_collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headCell4" forIndexPath:indexPath];
        }
    }
    reusableView.backgroundColor = [UIColor whiteColor];
    [reusableView addSubview:noteLabel];
    return reusableView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(ScreenX375(280), ScreenX375(39));
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *key = _dataArray[indexPath.section][@"dataArray"][indexPath.row];
    CGRect rect = [key boundingRectWithSize:CGSizeMake(ScreenX375(200), ScreenX375(30)) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName:Kfont(12)} context:nil];
    return CGSizeMake(rect.size.width + ScreenX375(28), ScreenX375(30));
}

@end


@implementation FilterViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, ScreenX375(30))];
        _textLabel.textAlignment = NSTextAlignmentCenter;
        _textLabel.font = Kfont(12);
        [self addSubview:_textLabel];
    }
    return self;
}

@end
